package personInfo;

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import person.DayList;
import person.Interval;

public class PersonTableModel extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8942477998782083564L;
	private String[] columnNames = { "Day", "Leave-Time", "Home-Time", "Active" };
	private Vector<Object[]> data;

	
	public void clearModel()
	{
		data = new Vector<Object[]>();
		for (int i = 0; i < 7; i++) {
			Object[] line = new Object[4];
			line[0] = DayList.days[i];
			line[1] = new String();
			line[2] = new String();
			line[3] = new Boolean(false);
			data.add(line);
		}
	}
	
	public PersonTableModel() {

		data = new Vector<Object[]>();
		for (int i = 0; i < 7; i++) {
			Object[] line = new Object[4];
			line[0] = DayList.days[i];
			line[1] = new String();
			line[2] = new String();
			line[3] = new Boolean(false);
			data.add(line);
		}
	}

	@Override
	public Class<? extends Object> getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	public int getColumnCount() {
		return 4;
	}

	@Override
	public String getColumnName(int col) {
		return columnNames[col];
	}

	@Override
	public int getRowCount() {
		return data.size();

	}

	@Override
	public Object getValueAt(int row, int col) {
		return data.get(row)[col];
	}

	@Override
	public boolean isCellEditable(int row, int col) {
		if (col != 0)
			if (data.get(row)[3] == Boolean.TRUE || col == 3)
				return true;
		return false;
	}

	@Override
	public void setValueAt(Object value, int row, int col) {
		if (col == 1 || col == 2) {
			boolean res = Interval.validate((String) value);
			if (res == true) {
				data.get(row)[col] = value;
				fireTableCellUpdated(row, col);
			} else {
				fireTableCellUpdated(row, col);
			}
		} else {
			data.get(row)[col] = value;
			fireTableCellUpdated(row, col);
		}
		
	}

}
